package com.yf.response;

import lombok.Getter;
import lombok.Setter;

/**
 * @author chentianwei
 * @date 2023/4/19 13:47
 * @description 返回体
 */
@Getter
@Setter
public class ResponseBody<T> {

    private Integer code;
    private String cause;
    private String msg;
    public T data;

    public static <T> ResponseBody<T> success(String msg,T data){
        ResponseBody<T> responseBody = new ResponseBody<>();
        responseBody.setCode(200);
        responseBody.setMsg(msg);
        responseBody.setCause(null);
        responseBody.setData(data);
        return responseBody;
    }

    public static <T> ResponseBody<T> success(String msg){
        ResponseBody<T> responseBody = new ResponseBody<>();
        responseBody.setCode(200);
        responseBody.setMsg(msg);
        responseBody.setCause(null);
        responseBody.setData(null);
        return responseBody;
    }

    public static <T> ResponseBody<T> success(T data){
        ResponseBody<T> responseBody = new ResponseBody<>();
        responseBody.setCode(200);
        responseBody.setMsg("success");
        responseBody.setCause(null);
        responseBody.setData(data);
        return responseBody;
    }

    public static <T> ResponseBody<T> error(T data,Throwable e){
        ResponseBody<T> responseBody = new ResponseBody<>();
        responseBody.setCode(530);
        responseBody.setCause(e.getClass().getName());
        responseBody.setMsg(e.getMessage());
        responseBody.setData(data);
        return responseBody;
    }

    public static <T> ResponseBody<T> error(Throwable e){
        ResponseBody<T> responseBody = new ResponseBody<>();
        responseBody.setCode(530);
        responseBody.setCause(e.getClass().getName());
        responseBody.setMsg(e.getMessage());
        responseBody.setData(null);
        return responseBody;
    }





}
